package com.loong.web.suppert.intercept.cookie;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.loong.web.suppert.auth.User;
import com.loong.web.suppert.auth.UserCookies;
import com.loong.web.suppert.auth.UserSessions;

/**
 * Cookie处理器
 *
 * @author 张成轩
 */
public class CookieController {

	/** Cookie数据读取器 */
	private CookieDataReader cookieDataReader;

	/**
	 * Cookiec处理
	 * 
	 * @param request 请求
	 * @param response 响应
	 */
	public void control(HttpServletRequest request, HttpServletResponse response) {

		if (cookieDataReader == null)
			return;
		User user = UserSessions.getUser(request);
		if (user == null) {
			String uid = UserCookies.getUser(request, response);
			if (uid == null)
				return;
			user = cookieDataReader.getUser(request, uid);
			if (user == null)
				UserCookies.removeUser(request, response);
			else
				UserSessions.setUser(request, user);
		}
	}

	/**
	 * @param cookieDataReader Cookie数据读取器
	 */
	public void setCookieDataReader(CookieDataReader cookieDataReader) {

		this.cookieDataReader = cookieDataReader;
	}
}
